package com.example.androidmvp;

import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class ContactsActivity extends android.app.Activity implements View.OnClickListener, ContactsView {
    private ProgressBar progressBar;
    private TextView mtv;

    private ContactsPresenter mPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contacts);
        progressBar = (ProgressBar) findViewById(R.id.pb);
        progressBar.setVisibility(View.INVISIBLE);
        mtv = (TextView) findViewById(R.id.tv);
        findViewById(R.id.btn).setOnClickListener(this);

        mPresenter = new ContactsPresenterImpl(this);
    }

    int index = 1;

    @Override
    public void onClick(View v) {
        index = index > 5 ? 1 : index;
        mPresenter.getContacts(index, getApplicationContext());
        index++;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.onDestroy();
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.INVISIBLE);
    }

    @Override
    public void setNotError() {
        mtv.setText("NotError");
    }

    @Override
    public void setNullError() {
        mtv.setText("NullError");
    }

    @Override
    public void navigateToHome() {
        mtv.setText("数据请求成功！");
    }

    @Override
    public void getContactsSuccess(List<SortModel> model) {
        mtv.setText("数据:\n" + model.toString());
    }
}
